<?php

/**
 * Created by PhpStorm.
 * User: zuoa
 * Date: 2016/11/23 0023
 * Time: 15:47
 */
class Y_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('record_model');
        $this->load->model('accout_model');
        $this->load->model('preset_model');
        $this->load->model('violation_model');
        $this->load->helper('url');
        $this->load->library('session');
        $this->load->library('pagination');

        $this->record_model->userdata = $this->session->userdata();
        $this->user_model->userdata = $this->session->userdata();

    }

    protected function _get_context()
    {
        $data["uncheck_count"] = $this->user_model->get_uncheck_count();
        $data["module"] = $product_id = $this->uri->segment(1);
        $data["sub_module"] = $product_id = $this->uri->segment(2);

        $data["require_locate_count"] = $this->record_model->get_require_locate_num();
        $data["require_type_in_count"] = $this->record_model->get_require_type_in_num();


        $data['username'] = $this->session->userdata('username');
        $data['userdept'] = $this->session->userdata('userdept');
        $data['is_admin'] = $this->session->userdata('is_admin');

        return $data;
    }

    public function response($code, $msg="success", $data =null)
    {
        $resp = array("code" => $code, "message" => $msg, "data" => $data);
        $this->output
            ->set_status_header(200)
            ->set_content_type('application/json', 'utf-8')
            ->set_output(json_encode($resp, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
            ->_display();
        exit;
    }

    public function is_post(){
        return $this->input->method() == "post";
    }

    public function check_login(){
        $userdata =  $this->session->userdata();
        if (!$userdata["uid"]) {
            if ($this->is_post()){
                $this->response(201, "未登录", null);
            }
            else{
                redirect("/dashboard/login");
            }
        }
    }

    protected function is_admin()
    {
        $user_data =  $this->session->userdata();
        return $user_data != null && (((int)$user_data["is_admin"]) == 1);
    }

    protected function get_dept($is_return_admin_default = true)
    {
        if ($is_return_admin_default && $this->is_admin()) {
            return '';
        }
        return $this->session->userdata('userdept');
    }


}